//
//  TagGridView.m
//  TestView
//
//  Created by wsj2015 on 16/8/30.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "TagGridView.h"
#import "Masonry.h"

#import "UIView+YTXAnimateCSS.h"
#import "tagCell.h"

@interface TagGridView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *tagLabel;
@property (weak, nonatomic) IBOutlet UIButton *indeBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic,  strong) NSArray *dataArray;


@end

@implementation TagGridView
{
    UIView * _maskView;
    
    
    

}


+ (instancetype)gridViewWithDataArray:(NSArray<NSString *> *)array{

    
    
    TagGridView *v = [[NSBundle mainBundle] loadNibNamed:@"TagGridView" owner:nil options:0].lastObject;
    v.tagLabel.alpha = 0;
    v.indeBtn.alpha = 0;
    [v.collectionView registerNib:[UINib nibWithNibName:@"tagCell" bundle:nil] forCellWithReuseIdentifier:@"tagCell"];
    UICollectionViewFlowLayout *layout =  (UICollectionViewFlowLayout *)v.collectionView.collectionViewLayout;
    
    v.dataArray = [NSArray arrayWithArray:array];
    v.selectIndex = 0;
    v.collectionView.delegate = v;
    v.collectionView.dataSource = v;
    CGFloat space = 10;
    layout.sectionInset =UIEdgeInsetsMake(6, space, space, space);
    layout.minimumInteritemSpacing = 15 ;
    layout.minimumLineSpacing = 15;
    v.indeBtn.transform = CGAffineTransformRotate(v.indeBtn.transform, M_PI);
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 15 * 2 -space * 2) /3.0, 39);
    
    return v;
}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    tagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tagCell" forIndexPath:indexPath];
    
    NSString *str =self.dataArray[indexPath.row];
    [collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    cell.tagLabel.text = str;
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.callBack) {
        self.callBack(indexPath.row);
    }
    [self disMiss];
   
}

#pragma show or dismiss

- (void)showInView:(UIView * )superView{

    _maskView = [UIView new];
    _maskView.frame = superView.bounds;
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
    [_maskView addGestureRecognizer:tap];
    _maskView.alpha = 0;
    _maskView.backgroundColor = [UIColor colorWithWhite:.8 alpha:.5];
    [superView addSubview:_maskView];
    
    [superView addSubview:self];
    
    [_maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superView);
        make.right.equalTo(superView);
        make.top.equalTo(superView).offset(0);
        make.bottom.equalTo(superView);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).offset(0);
        make.left.equalTo(superView);
        make.right.equalTo(superView);
        make.height.mas_equalTo(0);
    }];
    
    
    [UIView animateWithDuration:.35f animations:^{
        _maskView.alpha = .8;
    } completion:^(BOOL finished) {
        
        self.tagLabel.alpha =1;
        self.indeBtn.alpha = 1;
         [self.indeBtn ytx_slideInRightAnimtionWithDurationTime:.8];
         [self.tagLabel ytx_slideInLeftAnimtionWithDurationTime:.8];
       
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(200);
        }];
        [UIView animateWithDuration:.35f animations:^{
            [self.superview layoutIfNeeded];
          
        } completion:^(BOOL finished) {
            
        }];
    }];
    

   
}

- (void)disMiss{
   
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
    }];
    [UIView animateWithDuration:.35f animations:^{
       
        [self.superview layoutIfNeeded];
       
       
    } completion:^(BOOL finished) {
        [self.indeBtn ytx_slideOutRightAnimtionWithDurationTime:.5];
        [self.tagLabel ytx_slideOutLeftAnimtionWithDurationTime:.5];
       
        [UIView animateWithDuration:.5 delay:0 options:0 animations:^{
             _maskView.alpha = 0.003;
            self.tagLabel.alpha = 0;
            self.indeBtn.alpha =0;
        } completion:^(BOOL finished) {
               [self removeFromSuperview];
            [_maskView removeFromSuperview];
            _maskView = nil;
         
        }];
        
      
    }];


}




@end
